// exports.AL_Framework_Release_Manager = void 0;
// var AL_GAME_HUA_Decorate = require("./AL_GAME_HUA_Decorate");
// var s = (function () {
//     function e() {
//         this.trace_assets_set = new Set();
//         this.extend();
//     }
//     e.prototype.extend = function () {
//         cc.Component.prototype.autoReleaseAssets = null;
//         cc.Component.prototype.addAutoReleaseAssets = function (e) {
//             var t = [];
//             for (var n = 1; n < arguments.length; n++) {
//                 t[n - 1] = arguments[n];
//             }
//             if (e) {
//                 if (!this.autoReleaseAssets) {
//                     this.autoReleaseAssets = new Set();
//                 }
//                 if (!Array.isArray(e)) {
//                     e = [e];
//                 }
//                 if (t && t.length > 0) {
//                     e = __spreadArrays(e, t);
//                 }
//                 var o = 0;
//                 for (var i = e; o < i.length; o++) {
//                     var a = i[o];
//                     if (cc.isValid(a) && !this.autoReleaseAssets.has(a)) {
//                         a.addRef();
//                         this.autoReleaseAssets.add(a);
//                     }
//                 }
//             }
//         };
//         var e = cc.Component.prototype._onPreDestroy;
//         cc.Component.prototype._onPreDestroy = function () {
//             if (this.autoReleaseAssets && this.autoReleaseAssets.size > 0) {
//                 this.autoReleaseAssets.forEach(function (e) {
//                     if (cc.isValid(e)) {
//                         if (!(e.refCount > 0)) {
//                             return;
//                         }
//                         e.decRef();
//                         var t = e._uuid;
//                         if (e["@bunlde"]) {
//                             var n = cc.assetManager.getBundle(e["@bunlde"]);
//                             if (n) {
//                                 var o = n.getAssetInfo(t);
//                                 if (o) {
//                                     o.path
//                                         ? ((t = n.base + o.path), e._native && (t += e._native))
//                                         : o.url && (t = o.url);
//                                 }
//                             }
//                         }
//                         e.refCount;
//                     }
//                 });
//                 this.autoReleaseAssets.clear();
//                 this.autoReleaseAssets = null;
//             }
//             if (e) {
//                 e.call(this);
//             }
//         };
//         this.onDestroyTemp = e;
//     };
//     e.prototype.reset = function () {
//         cc.Component.prototype._onPreDestroy = this.onDestroyTemp;
//     };
//     e.prototype.zhuizong_ziyuan = function (e) {
//         if (e) {
//             if (!this.trace_assets_set.has(e)) {
//                 e.addRef();
//                 this.trace_assets_set.add(e);
//                 this.kuozhan_ziyuan(e);
//             }
//         }
//     };
//     e.prototype.tiaoshi_ziyuan = function (e) {
//         var t = e.addRef;
//         var n = e.decRef;
//         e.addRef = function () {
//             var n = [];
//             for (var o = 0; o < arguments.length; o++) {
//                 n[o] = arguments[o];
//             }
//             var i = t.apply(e, n);
//             console.log("[addRef]:" + e.name + "-" + e.refCount);
//             return i;
//         };
//         e.decRef = function () {
//             var t = [];
//             for (var o = 0; o < arguments.length; o++) {
//                 t[o] = arguments[o];
//             }
//             var i = n.apply(e, t);
//             console.log("[decRef]:" + e.name + "-" + e.refCount);
//             return i;
//         };
//     };
//     e.prototype.kuozhan_ziyuan = function (e) {
//         var t = e.addRef;
//         var n = e.decRef;
//         var o = new Map();
//         e.traceMap = o;
//         var i = this;
//         e.addRef = function () {
//             var n = [];
//             for (var r = 0; r < arguments.length; r++) {
//                 n[r] = arguments[r];
//             }
//             var a = i.huoqu_diaoyong_zhan(1);
//             var s = o.has(a) ? o.get(a) + 1 : 1;
//             o.set(a, s);
//             var c = t.apply(e, n);
//             console.log("[addRef]:" + e.name + "-" + e.refCount);
//             return c;
//         };
//         e.decRef = function () {
//             var t = [];
//             for (var r = 0; r < arguments.length; r++) {
//                 t[r] = arguments[r];
//             }
//             var a = i.huoqu_diaoyong_zhan(1);
//             var s = o.has(a) ? o.get(a) + 1 : 1;
//             o.set(a, s);
//             var c = n.apply(e, t);
//             console.log("[decRef]:" + e.name + "-" + e.refCount);
//             return c;
//         };
//         e.resetTrace = function () {
//             e.addRef = t;
//             e.decRef = n;
//             delete e.traceMap;
//         };
//     };
//     e.prototype.chongzhi_ziyuan = function (e) {
//         if (e.resetTrace) {
//             e.resetTrace();
//         }
//     };
//     e.prototype.chongzhi_suoyou_ziyuan = function () {
//         var e = this;
//         this.trace_assets_set.forEach(function (t) {
//             e.chongzhi_ziyuan(t);
//             t.decRef();
//         });
//         this.trace_assets_set.clear();
//     };
//     e.prototype.tingzhi_zhuizong_ziyuan = function (e) {
//         if (this.trace_assets_set.has(e)) {
//             this.chongzhi_ziyuan(e);
//             e.decRef();
//             this.trace_assets_set.delete(e);
//         }
//     };
//     e.prototype.huoqu_suoyou_zhuizong_de_ziyuan = function () {
//         return this.trace_assets_set;
//     };
//     e.prototype.debug = function () {
//         this.trace_assets_set.forEach(function (e) {
//             var t = e.traceMap;
//             if (t) {
//                 t.forEach(function (e, t) {
//                     Log.d(e + " : " + t + " ");
//                 });
//             }
//         });
//     };
//     e.prototype.huoqu_diaoyong_zhan = function (e) {
//         var t = new Error().stack;
//         var n = this.chazhao_zifu_zai_zifuchuan_zhong_weizhi(t, "\n", e);
//         if (n > 0) {
//             t = t.slice(n);
//         }
//         return t;
//     };
//     e.prototype.chazhao_zifu_zai_zifuchuan_zhong_weizhi = function (e, t, n) {
//         var o = e.indexOf(t);
//         var i = o;
//         for (var r = 0; r < n; r++) {
//             if (-1 == (o = e.indexOf(t, o + 1))) {
//                 return i;
//             }
//             i = o;
//         }
//         return i;
//     };
//     return __decorate([AL_GAME_HUA_Decorate.Decorate.instance], e);
// })();
// exports.AL_Framework_Release_Manager = s;
// s.Instance;
// window.debugReleaseManager = function () {
//     s.Instance.debug.apply(s.Instance);
// };

exports.AL_Framework_Release_Manager = void 0;
const { Decorate } = require("./AL_GAME_HUA_Decorate");

/**
 * ReleaseManager类，用于管理资源的引用与释放。
 */
class AL_Framework_Release_Manager {
    constructor() {
        /** @type {Set} trace_assets_set 追踪的资源集合 */
        this.trace_assets_set = new Set();
        this.extend();
    }

    /**
     * 扩展 cc.Component 方法，增加自动释放资源的功能
     */
    extend() {
        cc.Component.prototype.autoReleaseAssets = null;

        /**
         * 增加自动释放资源
         * @param {Array|any} assets - 要自动释放的资源
         */
        cc.Component.prototype.addAutoReleaseAssets = function (assets, ...additionalAssets) {
            if (assets) {
                if (!this.autoReleaseAssets) {
                    this.autoReleaseAssets = new Set();
                }
                if (!Array.isArray(assets)) {
                    assets = [assets];
                }
                if (additionalAssets.length > 0) {
                    assets = [...assets, ...additionalAssets];
                }
                for (const asset of assets) {
                    if (cc.isValid(asset) && !this.autoReleaseAssets.has(asset)) {
                        asset.addRef();
                        this.autoReleaseAssets.add(asset);
                    }
                }
            }
        };

        const originalPreDestroy = cc.Component.prototype._onPreDestroy;
        cc.Component.prototype._onPreDestroy = function () {
            if (this.autoReleaseAssets && this.autoReleaseAssets.size > 0) {
                this.autoReleaseAssets.forEach((asset) => {
                    if (cc.isValid(asset) && asset.refCount > 0) {
                        asset.decRef();
                        let uuid = asset._uuid;
                        if (asset["@bunlde"]) {
                            const bundle = cc.assetManager.getBundle(asset["@bunlde"]);
                            if (bundle) {
                                const assetInfo = bundle.getAssetInfo(uuid);
                                if (assetInfo) {
                                    uuid = assetInfo.path ? (bundle.base + assetInfo.path + (asset._native || "")) : assetInfo.url || uuid;
                                }
                            }
                        }
                        asset.refCount; // 保留引用计数
                    }
                });
                this.autoReleaseAssets.clear();
                this.autoReleaseAssets = null;
            }
            if (originalPreDestroy) {
                originalPreDestroy.call(this);
            }
        };

        this.onDestroyTemp = originalPreDestroy;
    }

    /**
     * 重置 cc.Component 的 _onPreDestroy 方法
     */
    reset() {
        cc.Component.prototype._onPreDestroy = this.onDestroyTemp;
    }

    /**
     * 追踪资源
     * @param {any} asset - 要追踪的资源
     */
    zhuizong_ziyuan(asset) {
        if (asset && !this.trace_assets_set.has(asset)) {
            asset.addRef();
            this.trace_assets_set.add(asset);
            this.kuozhan_ziyuan(asset);
        }
    }

    /**
     * 调试资源
     * @param {any} asset - 要调试的资源
     */
    tiaoshi_ziyuan(asset) {
        const originalAddRef = asset.addRef;
        const originalDecRef = asset.decRef;

        asset.addRef = function (...args) {
            const result = originalAddRef.apply(asset, args);
            console.log("[addRef]:" + asset.name + "-" + asset.refCount);
            return result;
        };

        asset.decRef = function (...args) {
            const result = originalDecRef.apply(asset, args);
            console.log("[decRef]:" + asset.name + "-" + asset.refCount);
            return result;
        };
    }

    /**
     * 扩展资源，增加追踪功能
     * @param {any} asset - 要扩展的资源
     */
    kuozhan_ziyuan(asset) {
        const originalAddRef = asset.addRef;
        const originalDecRef = asset.decRef;
        const traceMap = new Map();
        asset.traceMap = traceMap;

        asset.addRef = (...args) => {
            const callStack = this.huoqu_diaoyong_zhan(1);
            traceMap.set(callStack, (traceMap.get(callStack) || 0) + 1);
            const result = originalAddRef.apply(asset, args);
            console.log("[addRef]:" + asset.name + "-" + asset.refCount);
            return result;
        };

        asset.decRef = (...args) => {
            const callStack = this.huoqu_diaoyong_zhan(1);
            traceMap.set(callStack, (traceMap.get(callStack) || 0) + 1);
            const result = originalDecRef.apply(asset, args);
            console.log("[decRef]:" + asset.name + "-" + asset.refCount);
            return result;
        };

        asset.resetTrace = function () {
            asset.addRef = originalAddRef;
            asset.decRef = originalDecRef;
            delete asset.traceMap;
        };
    }

    /**
     * 重置资源的追踪
     * @param {any} asset - 要重置的资源
     */
    chongzhi_ziyuan(asset) {
        if (asset.resetTrace) {
            asset.resetTrace();
        }
    }

    /**
     * 重置所有追踪的资源
     */
    chongzhi_suoyou_ziyuan() {
        this.trace_assets_set.forEach((asset) => {
            this.chongzhi_ziyuan(asset);
            asset.decRef();
        });
        this.trace_assets_set.clear();
    }

    /**
     * 停止追踪资源
     * @param {any} asset - 要停止追踪的资源
     */
    tingzhi_zhuizong_ziyuan(asset) {
        if (this.trace_assets_set.has(asset)) {
            this.chongzhi_ziyuan(asset);
            asset.decRef();
            this.trace_assets_set.delete(asset);
        }
    }

    /**
     * 获取所有追踪的资源
     * @returns {Set} - 追踪的资源集合
     */
    huoqu_suoyou_zhuizong_de_ziyuan() {
        return this.trace_assets_set;
    }

    /**
     * 调试所有追踪的资源
     */
    debug() {
        this.trace_assets_set.forEach((asset) => {
            const traceMap = asset.traceMap;
            if (traceMap) {
                traceMap.forEach((value, key) => {
                    console.log(value + " : " + key + " ");
                });
            }
        });
    }

    /**
     * 获取调用栈
     * @param {number} depth - 调用层级深度
     * @returns {string} - 调用栈字符串
     */
    huoqu_diaoyong_zhan(depth) {
        const stack = new Error().stack;
        return this.slice_Stack(stack, "\n", depth);
    }

    /**
     * 获取字符在字符串中的位置
     * @param {string} str - 字符串
     * @param {string} char - 要查找的字符
     * @param {number} count - 查找的次数
     * @returns {number} - 字符位置
     */
    chazhao_zifu_zai_zifuchuan_zhong_weizhi(str, char, count) {
        let pos = str.indexOf(char);
        let lastPos = pos;
        for (let i = 0; i < count; i++) {
            pos = str.indexOf(char, pos + 1);
            if (pos === -1) {
                return lastPos;
            }
            lastPos = pos;
        }
        return lastPos;
    }

    /**
     * 切分调用栈字符串
     * @param {string} stack - 调用栈字符串
     * @param {string} delimiter - 分隔符
     * @param {number} depth - 深度
     * @returns {string} - 切分后的调用栈字符串
     */
    slice_Stack(stack, delimiter, depth) {
        const pos = this.chazhao_zifu_zai_zifuchuan_zhong_weizhi(stack, delimiter, depth);
        return pos > 0 ? stack.slice(pos) : stack;
    }
}

__decorate([Decorate.instance], AL_Framework_Release_Manager);

exports.AL_Framework_Release_Manager = AL_Framework_Release_Manager;
AL_Framework_Release_Manager.Instance;
window.debugReleaseManager = function () {
    AL_Framework_Release_Manager.Instance.debug.apply(AL_Framework_Release_Manager.Instance);
};
